<template>
  <div class="cla_home">
    <el-container class="home-container">
      <!-- 头部区域 -->
      <el-header>
        <div>
          <img src="../assets/img/heima.png" alt />
          <span>电商管理系统</span>
        </div>
        <el-button type="info" @click="exit" round>退出</el-button>
      </el-header>
      <el-container>
        <!-- 左边侧边栏区域 -->
        <el-aside :width="isCollapse ? '64px' : '180px'">
          <div class="toggle-button" @click="toggleClick">| | |</div>
          <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409fff"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            router
            :default-active="menuSelectPath"
          >
            <!-- 一级菜单 -->
            <el-submenu :index="item.id + '' " v-for="item in menuList" :key="item">
              <!-- 一级菜单模板 -->
              <template slot="title">
                <i :class="iocnObj[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                :index="'/' + subItem.path"
                v-for="subItem in item.children"
                :key="subItem.id"
                @click="menuSelect('/' + subItem.path)"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ subItem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>

        <!-- 内容主体区域 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
export default {
  created() {
    this.getMenuList();
    var msp = window.sessionStorage.getItem('menuSelPath'); 
    this.menuSelectPath = msp;
  },
  data() {
    return {
      menuList: [],
      iocnObj: {
        "125": "iconfont icon-user",
        "103": "iconfont icon-3702mima",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao"
      },

      isCollapse: false,
      menuSelectPath: ""
    };
  },
  methods: {
    toggleClick() {
      this.isCollapse = !this.isCollapse;
    },
    menuSelect(menuSelPath) {
      window.sessionStorage.setItem("menuSelPath", menuSelPath);
      this.menuSelectPath = menuSelPath;
    },
    exit() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    getMenuList() {
      this.$httpReq
        .get("menus")
        .then(res => {
          console.log("===菜单数据=>" + JSON.stringify(res));
          if (res.meta.status == 200) {
            this.menuList = res.data;
          } else {
            var msg = res.meta.msg;
            this.$message.error(msg);
          }
        })
        .catch(error => {
          console.log("===菜单数据=>" + JSON.stringify(error.data));
           this.$message.error(JSON.stringify(error.data));
        });
    }
  }
};
</script>

<style scoped>
.cla_home {
  width: 100%;
  height: 100%;
  background: red;
}
.exit {
  margin-left: 90%;
  font-size: 24px;
  color: red;
}
.home-container {
  width: 100%;
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  color: #fff;
  justify-content: space-between;
  align-items: center;
  padding-left: 0px;
  font-size: 20px;
}

.el-header div {
  display: flex;
  align-items: center;
}
.el-header span {
  margin-left: 10px;
}

.el-aside {
  background-color: #333744;
}

.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.el-menu {
  border-right: solid 0px #e6e6e6;
}
.toggle-button {
  background: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: floralwhite;
  text-align: center;
  cursor: pointer;
}
</style>